package cn.smbms.service.bill;

import cn.smbms.dao.bill.BillDao;
import cn.smbms.pojo.Bill;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.util.List;

@Service("bill")
public class BillServiceImpl implements BillService {
	SqlSessionFactory sqlSessionFactory=null;
	@Resource
	private BillDao billDao;
	public BillServiceImpl(){
		//billDao = new BillDaoImpl();
	}
	@Override
	public boolean add(Bill bill) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection connection = null;
		SqlSession session=null;
		try {
//			connection = BaseDao.getConnection();
//			connection.setAutoCommit(false);//开启JDBC事务管理
//			if(billDao.add(connection,bill) > 0)
//				flag = true;
//			connection.commit();
//			sqlSessionFactory= utils.open();
//			session=sqlSessionFactory.openSession(true);
//			int sum=session.getMapper(BillDao.class).add(bill);
			int sum=billDao.add(bill);
			if(sum>0) {
				System.out.println("数"+sum);
				flag = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//			try {
//				System.out.println("rollback==================");
//				connection.rollback();
//			} catch (SQLException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
		}finally{
			//在service层进行connection连接的关闭
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return flag;
	}

	@Override
	public List<Bill> getBillList(Bill bill) {
		// TODO Auto-generated method stub
		Connection connection = null;
		List<Bill> billList = null;
		SqlSession session=null;
		System.out.println("query productName ---- > " + bill.getProductName());
		System.out.println("query providerId ---- > " + bill.getProviderId());
		System.out.println("query isPayment ---- > " + bill.getIsPayment());
		try {
//			connection = BaseDao.getConnection();
//			billList = billDao.getBillList(connection, bill);
//			sqlSessionFactory= utils.open();
//			session=sqlSessionFactory.openSession(true);
//			billList=session.getMapper(BillDao.class).getBillList(bill);
			billList=billDao.getBillList(bill);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return billList;
	}

	@Override
	public boolean deleteBillById(String delId) {
		// TODO Auto-generated method stub
		Connection connection = null;
		boolean flag = false;
		SqlSession session=null;
		try {
//			connection = BaseDao.getConnection();
//			sqlSessionFactory= utils.open();
//			session=sqlSessionFactory.openSession(true);
//			int sum=session.getMapper(BillDao.class).deleteBillById(delId);
			int sum=billDao.deleteBillById(delId);
			if(sum> 0) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return flag;
	}

	@Override
	public Bill getBillById(String id) {
		// TODO Auto-generated method stub
		Bill bill = null;
		Connection connection = null;
		SqlSession session=null;
		try{
//			connection = BaseDao.getConnection();
////			bill = billDao.getBillById(connection, id);
//			sqlSessionFactory= utils.open();
//			session=sqlSessionFactory.openSession(true);
//			bill=session.getMapper(BillDao.class).getBillById(id);
			bill=billDao.getBillById(id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			bill = null;
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return bill;
	}

	@Override
	public boolean modify(Bill bill) {
		// TODO Auto-generated method stub
		Connection connection = null;
		boolean flag = false;
		SqlSession session=null;
		try {
//			connection = BaseDao.getConnection();
//			sqlSessionFactory= utils.open();
//			session=sqlSessionFactory.openSession(true);
//			int sum=session.getMapper(BillDao.class).modify(bill);
			int sum=billDao.modify(bill);
			if(sum> 0) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return flag;
	}

}
